using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace KFGame.Modes
{
    public class KFSeralPortConfigMode : KFMode
    {
 
        public static KFSeralPortConfigMode getModes()
        {
               if (!editObject)   
               editObject = new GameObject("KF_Config_edit");
            var mode =  editObject.AddComponent<KFSeralPortConfigMode>();
            return mode;
        }

        public string filename = "kfseralportconfig.ini";


        /// <summary>
        /// 所有事件
        /// </summary>
        /// <typeparam name="KFKeyEventMode"></typeparam>
        /// <returns></returns>
        [SerializeField]
        [Tooltip("IO接口记录集")]
        public List<KFSeralPortMode> eventModes = new List<KFSeralPortMode>();

        public override string FileName()
        {
            return filename;
        }

        public override void save()
        {
            string iniPath = Application.streamingAssetsPath + "/KFConfig/" + filename;
            INIParser iNIParser = new INIParser();
            iNIParser.Open(iniPath);
            iNIParser.clean();

            for (var i = 0; i < eventModes.Count; i++)
            {
                if (eventModes[i].modeName == null)
                    eventModes[i].modeName = eventModes[i].name + i;

                iNIParser.WriteValue(eventModes[i].modeName, "enter", eventModes[i].enter);
                iNIParser.WriteValue(eventModes[i].modeName, "name", eventModes[i].name);

                iNIParser.WriteValue(eventModes[i].modeName, "comName", eventModes[i].comName);
                iNIParser.WriteValue(eventModes[i].modeName, "baudRate", eventModes[i].baudRate);
                iNIParser.WriteValue(eventModes[i].modeName, "dataBit", eventModes[i].dataBit);
                iNIParser.WriteValue(eventModes[i].modeName, "flowControl", eventModes[i].flowControl);
                iNIParser.WriteValue(eventModes[i].modeName, "parity", eventModes[i].parity);
                iNIParser.WriteValue(eventModes[i].modeName, "stopBit", eventModes[i].stopBit);
                iNIParser.WriteValue(eventModes[i].modeName, "formatType", eventModes[i].formatType);
                iNIParser.WriteValue(eventModes[i].modeName, "formatLangth", eventModes[i].formatLangth);
                iNIParser.WriteValue(eventModes[i].modeName, "formatStart", eventModes[i].formatStart);
                iNIParser.WriteValue(eventModes[i].modeName, "formatEnd", eventModes[i].formatEnd);
                iNIParser.WriteValue(eventModes[i].modeName, "formatSizeIndex", eventModes[i].formatSizeIndex);
                iNIParser.WriteValue(eventModes[i].modeName, "formatParity", eventModes[i].formatParity);
                iNIParser.WriteValue(eventModes[i].modeName, "formatSendParity", eventModes[i].formatSendParity);
                
                iNIParser.WriteValue(eventModes[i].modeName, "des", eventModes[i].des);


            }
            iNIParser.save();

        }

        public override void LoadDatas()
        {
            string iniPath = Application.streamingAssetsPath + "/KFConfig/" + filename;
            INIParser iNIParser = new INIParser();
            iNIParser.Open(iniPath);
            foreach (var item in iNIParser.SectionNames())
            {
                KFSeralPortMode mode = new KFSeralPortMode();
                mode.modeName = item;

                mode.enter = iNIParser.ReadValue(item, "enter", true);
                mode.name = iNIParser.ReadValue(item, "name", "");
                mode.comName = iNIParser.ReadValue(item, "comName", "");
                mode.baudRate = iNIParser.ReadValue(item, "baudRate", 9600);
                mode.dataBit = iNIParser.ReadValue(item, "dataBit", 8);
                mode.flowControl = iNIParser.ReadValue(item, "flowControl", 0);
                mode.parity = iNIParser.ReadValue(item, "parity", 0);
                mode.stopBit = iNIParser.ReadValue(item, "stopBit", 1);
                mode.formatType = iNIParser.ReadValue(item, "formatType", "07");
                mode.formatLangth = iNIParser.ReadValue(item, "formatLangth", 18);
                mode.formatStart = iNIParser.ReadValue(item, "formatStart", "AA");
                mode.formatEnd = iNIParser.ReadValue(item, "formatEnd", "CD");
                mode.formatSizeIndex = iNIParser.ReadValue(item, "formatSizeIndex", 1);
                mode.formatParity = iNIParser.ReadValue(item, "formatParity", "");
                mode.formatSendParity = iNIParser.ReadValue(item, "formatSendParity", "CRC_HL");
                
                mode.des = iNIParser.ReadValue(item, "des", "");
                eventModes.Add(mode);



            }
        }
    }
}